#include "PlayerStateJump.h"
#include "Player.h"

PlayerStateJump::PlayerStateJump(Player* player, sf::Sound* jumpSFX) :
PlayerState(player),
_jumpSFX(jumpSFX)
{}


PlayerStateJump::~PlayerStateJump(void){}

void PlayerStateJump::activate()
{
	_player->removeFlag(GRAVITY_AFFECTED);
	_player->getAnimationMgr().setAnimation("jump");
	_player->_isJumping = true;
	_jumpSFX->play();
	_player->_onGround = false;
	_player->_velocity.y = 0.0f;
}

void PlayerStateJump::deactivate()
{

}

int PlayerStateJump::update()
{
	if (_player->damageReceived())
		return PSRV_TAKE_DAMAGE;
	if (_player->_isJumping)
		_player->_velocity.y = -(_player->getJumpPower() * GameTime::getSingleton()->deltaTime());

	if (_player->_position.y < _player->_jumpStartPosition + _player->getJumpLength())
	{
		_player->setFlag(GRAVITY_AFFECTED);
		_player->_isJumping = false;
	}
	else if (_player->getControlState()->isKeyReleased(CControls::Key(CTRL_JUMP, 0), 0))
	{
		_player->setFlag(GRAVITY_AFFECTED);
		_player->_isJumping = false;
	}

	_player->applyVelocity();

	if (_player->_velocity.y > 0.0f)
		return PSRV_FALLING;

	else if (_player->getControlState()->isKeyPressed(CControls::Key(CTRL_SHOOT, 0), 0))
	{
		return PSRV_ATTACK_AIR;
	}
	else if(_player->getControlState()->isKeyPressed(CControls::Key(CTRL_DASH, 0), 0) && _player->canDash())
	{
		_player->_velocity.y = 0.0f;
		_player->_isJumping = false;
		return PSRV_DASH_AIR;
	}
	else if (_player->getControlState()->isKeyDown(CControls::Key(CTRL_UP, 0),0) && ((_player->flags() & LADDER_COLLISION) == LADDER_COLLISION))
		return PSRV_CLIMB;

	return PSRV_NOTHING;
}
